<?php

$f3=require('../lib/base.php');

$f3->set('DEBUG',0);
$f3->set('UI','../views/');
$f3->config('../setup.cfg');
$f3->route('GET /',
           function($f3) {
             $page = str_replace("/?page=",'',$f3->get("PARAMS")[0]);
             $host = '';
             if (isset($page) && $page >0) 
               $host = 'http://iask.sina.com.cn/rank/finish_question.php?page='.$page;
             else
               $host = 'http://iask.sina.com.cn/rank/finish_question.php';

               $req = \Web::instance()->request($host);
               if (isset($req['body'])) {
                 $raw = iconv('GBK', 'UTF-8',$req['body']);
                 $raw = preg_replace('/[\n\r\t]/i','',$raw);
                 preg_match('/<table.*?<\/table>/i',$raw, $home);
                 $home = $home[0];
                 $home = preg_replace('/\/b\/([0-9]*)\.html/i','/questions/q$1.html',$home);
                 $home = preg_replace('/\/c\/([0-9]*)\.html/i','/categories/c$1.html',$home);
                 $home = preg_replace('/\/browse\/get_class\.php\?fatherid=([0-9]*)/i','/categories/c$1.html',$home);
                 $home = preg_replace('/<tr><td height="1"(.|\n)*?<\/tr>/i','',$home);
                 $home = str_replace('f12 c6 lh25','table table-striped',$home);
                 $home = preg_replace('/<td width="80"(.|\n)*?<\/td>/i','',$home);
                 $home = str_replace('width="65"','width="90"',$home);
                 $home = str_replace('爱问iAsk','YEP8网',$home);
                 $home = preg_replace('/<td colspan="9" height="1"(.|\n)*?<\/td>/i','<td></td>',$home);
                 $home = str_replace('<tr><td></td></tr>','',$home);
                 $home = str_replace('height="60"', 'height="30" style="padding-top:15px;"',$home);
                 $f3->set('home', $home);
               }

               $f3->set("css", "home.css");
               echo View::instance()->render('home.html');
           },3600
    );

$f3->route('GET /categories/*', 
           function ($f3) {
               $url = $f3->get("PARAMS")[1];
               $this_link = $url;
               $pages = explode("?",$this_link);
               $page = "page=0";
               if (count($pages)>1)
                   $page = $pages[1] ;
               
               $url = preg_replace('/^c([0-9]*)\.html(.*)$/i',"c/$1.html",$url);
               $url = "http://iask.sina.com.cn/".$url."?status=K&".$page;

               $breadcrumbs = "";
               $category = "";
               $list = "";

               $req = \Web::instance()->request($url);

               if (isset($req['body'])) {
                   $raw = iconv('gbk', 'utf-8',$req['body']);

                   preg_match('/<title>.*?<\/title>/i', $raw, $title);
                   preg_match('/<meta name="keywords" content="[\s\S]*?>/i', $raw, $keywords);
                   if(isset($title))
                       $title = str_replace(array("爱问知识人",">"),array("就爱问答网","/>"),$title);

                   if ($keywords != null)
                       $keywords = str_replace(array("爱问知识人",">"),array("就爱问答网","/>"),$keywords);

                   $raw = preg_replace('/\/b\/([0-9]*)\.html/i','/questions/q$1.html',$raw);
                   $raw = preg_replace('/\/c\/([0-9]*)\.html/i','/categories/c$1.html',$raw);

                   preg_match('/<div id="fl_xx"[\s\S]*?<\/div>/i',$raw,$category);
                   $f3->set("site_name", $title[0]);
                   $f3->set("site_keywords", $keywords[0]);
                   $f3->set('category', '');
                   $category = count($category) >0 ?  $category[0] : '';
                   preg_match('/<div class="b1_e0 lh25"[\s\S]*?<div class="p10" align="right">/i',$raw,$list);

                   preg_match('/<div id="nav2"[\s\S]*?<\/div>/i',$raw, $breadcrumbs);
                   $breadcrumbs = $breadcrumbs[0];
                   $breadcrumbs = str_replace("nav2", "breadcrumbs",$breadcrumbs);
                   $breadcrumbs = str_replace("/cate/",'/',$breadcrumbs);
                   $breadcrumbs = str_replace('问题分类','就爱问答网',$breadcrumbs);
                   $breadcrumbs = str_replace('</font>','',$breadcrumbs);
                   
                   $links = array();
                   preg_match_all('/<a href="(.*?)">(.*?)<\/a>/i', $breadcrumbs,$bbs);
                   for($i=0; $i< count($bbs[0]); $i++) {
                       $links[$i]["href"] = $bbs[1][$i];
                       $links[$i]["text"] = $bbs[2][$i];
                   }
                   
                   $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);

                   $list = str_replace('<div class="p10" align="right">','', $list[0]);
                   $list = str_replace('iask.sina.com.cn','www.yep8.com',$list);
                   $list = str_replace('<div class="p10" align="right">','',$list);
                   $list = preg_replace('/<div class="bb1e"[\s\S]*?<\/div>/i','',$list);
                   $list = str_replace('status=K&','',$list);
                   $list = preg_replace('/<p class="fl w50 c9".*?<\/p>/i','',$list);
                   $list = preg_replace('/<p class="fl w80">.*?<\/p>/i','',$list);
                   $list = str_replace('380px','520px',$list);
                   $list = str_replace('http://image2.sina.com.cn/pfp/ask/images/zhishi/','/assets/img/',$list);
                   $list = str_replace('爱问iAsk', 'YEP8网',$list);
  
                   $list = preg_replace('/[\n\r\t]/i','',$list);
                   $list = preg_replace('/>\s*</i','><',$list);
                   $list = str_replace('align="center', '',$list);
               }

               $f3->set("breadcrumbs",$breadcrumbs);
               $f3->set("category",$category);
               $f3->set("list",$list);
               $f3->set("css", "category.css");
               echo View::instance()->render('category.html');

           },3600
    );

$f3->route('GET /questions/*',
           function($f3) {
               $breadcrumbs = '';
               $questions = '';

               $url = $f3->get("PARAMS")[1];
               $this_link = $url;

               $url = preg_replace('/q([0-9]+)\.html(.*)/i',"http://iask.sina.com.cn/b/$1.html$2",$url);
               $req = \Web::instance()->request($url);

               if (isset($req['body'])) {
                   $raw = iconv('GBK', 'UTF-8', $req['body']);
                   $raw = preg_replace('/\n\r/i', '',$raw);
                   
                   $raw = preg_replace('/\/b\/([0-9]*)\.html/i','/questions/q$1.html',$raw);
                   $raw = preg_replace('/\/c\/([0-9]*)\.html/i','/categories/c$1.html',$raw);

                   preg_match('/<div class="nav22"[\s\S]*?<\/div>/i',$raw,$breadcrumbs);

                   if ($breadcrumbs) {
                       $breadcrumbs = str_replace("nav22", "breadcrumbs",$breadcrumbs);
                       $breadcrumbs = str_replace("/cate/",'"/"',$breadcrumbs);
                       $breadcrumbs = str_replace('问题分类','就爱问答网',$breadcrumbs);
                       $breadcrumbs = preg_replace('/<a [\s\S^>]*>知识人<\/a>\s*&gt;\s*/i','',$breadcrumbs);

                       $breadcrumbs = str_replace('/z/TopQuestion.htm','/', $breadcrumbs);
                       $breadcrumbs = str_replace("'", '"',$breadcrumbs);

                       $breadcrumbs = preg_replace('/\/z\/c([0-9]*)\.htm/i', '/categories/c$1.html', $breadcrumbs);
                       $links = array();
                       preg_match_all('/<a href="(.*?)">(.*?)<\/a>/i', $breadcrumbs[0],$bbs);
                       for($i=0; $i< count($bbs[0]); $i++) {
                           $links[$i]["href"] = $bbs[1][$i];
                           $links[$i]["text"] = $bbs[2][$i];
                       }

                       $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);

                   } else 
		     $breadcrumbs = '';
                   
                   $f3->set("breadcrumbs",$breadcrumbs);
                   preg_match('/<title>[\s\S]*?<\/title>/i', $raw, $title);
                   $title = str_replace('搜搜问问','YEP5问答网',$title[0]);
                   preg_match('/<meta name="keywords" content=".*"><\/meta>/i', $req['body'],$keywords);
                   if (isset($keywords[0]))
                       $keywords = str_replace(array("问问 搜搜 腾讯","></meta>"),array("YEP5 问答网", "/>"), $keywords[0]);
                   else 
                       $keywords = $f3->get('site_keywords');

                   $description = '<meta name="description" content="最全的知识问答网站，用户可以搜索其他用户沉淀的精彩内容，浏览到最精彩的问答内容."/>';

                   $f3->set("site_name", $title);
                   $f3->set("site_desc", $description);
                   $f3->set("site_keywords", $keywords);

                   $h1 = '<p class="lead">网友提问';
                   preg_match('/<h1>((.|\n)*?)<\/h1>/i',$raw,$tmp);

                   if ($tmp != null)
                       $h1 .= ': <a href="' . $this_link .'">'.  $tmp[1] . '</a>';


                   $h1 .='</p>';

                   
                   $questions = "";
                   preg_match_all('/<div class="pb5">[\s\S]*?<\/div>/i',$raw,$h1d);
                   if ($h1d[0] != null) {
                       $h1 .= $h1d[0][0];
                       for ($i=1;$i<count($h1d[0]);$i++) {
                           if(strpos($h1d[0][$i],'提问者对答案的评价')<= 0)
                               $questions .= '<div class="well"><p class="lead">回答者:<a href="http://www.yep8.com">就爱问答网</a></p><blockquote><p>'.$h1d[0][$i]."</p></blockquote></div>";
	    }
 
                   }
                       
                   $f3->set('h1',$h1);

                   $questions = preg_replace('/<script[\s\S]*?<\/script>/i','', $questions);
                   $questions = preg_replace('/[\n\r\t]/i','',$questions);
                   
               }

               $f3->set('questions', $questions);
               $f3->set('css', 'question.css');
               echo View::instance()->render('question.html');
           },3600
    );
$f3->run();
